package main

import (
	"fmt"
	"strconv"
	"strings"
)

var longMapShort = make(map[string]string)
var longUlrList []string
var index = 0
var prefix = "http://vtamins.com/"

func main() {
	url1 := "http://www.baidu.com"
	url2 := "http://www.google.com"

	shortUrl1 := encode(url1)
	shortUrl2 := encode(url2)

	fmt.Println(shortUrl1)
	fmt.Println(shortUrl2)
	fmt.Println(encode(url1))

	fmt.Println(decode(shortUrl1))
	fmt.Println(decode(shortUrl2))
}

func encode(longUrl string) string {
	if val, ok := longMapShort[longUrl]; ok {
		return val
	}
	shortUrl := prefix + strconv.Itoa(index)
	longMapShort[longUrl] = shortUrl
	longUlrList = append(longUlrList, longUrl)
	index++
	return shortUrl
}

func decode(shortUrl string) string {
	sp := strings.Split(shortUrl, "/")
	i, err := strconv.Atoi(sp[len(sp)-1])
	if err != nil {
		panic(err)
	}
	return longUlrList[i]
}
